package mt.client;

import java.awt.Container;
import java.awt.FlowLayout;
import java.util.LinkedList;

import javax.swing.*;

import mt.Order;
import mt.client.JTableModel.TableType;
import mt.client.listeners.DisconnectListener;
import mt.client.listeners.PurchaseListener;
import mt.client.listeners.SellListener;
import mt.comm.*;

/**
 * This is the Graphic Interface shown to the client after the login.
 * It shows all orders: Selling Orders, Purchasing Orders and History.
 * It is updated receiving messages from the Server.
 * @author tom93d
 *
 */
public class GraphicInterface {
	
	private static JMenuItem itemDisconnect = new JMenuItem("Disconnect");
	private static JMenu jMenu  = new JMenu("Menu");
	private static JMenuBar jMenuBar = new JMenuBar();

	private static JButton sellUnits = new JButton("Sell Units");
	private static JButton purchaseUnits = new JButton("Purchase Units");
	private static JTableModel jTableModelSell;
	private static JTableModel jTableModelHistory;
	private static JTableModel jTableModelPurchase;
	
	private static JTable jTableSell;
	private static JTable jTableHistory;
	private static JTable jTablePurchase;
	
	private static JScrollPane jScrollPaneSell;
	private static JScrollPane jScrollPanePurchase;
	private static JScrollPane jScrollPaneHistory;
	
	private static JSplitPane northEast;
	private static JSplitPane north;
	private static JSplitPane south;
	
	private static JPanel jPanelButtons;
	
	private static JFrame microTraderFrame;

	private String nickName;
	private ClientComm clientComm;

	private static final int height = 600;
	private static final int width = 600;
	
	private static LinkedList<Order> sellList = new LinkedList<Order>();
	private static LinkedList<Order> purchaseList = new LinkedList<Order>();
	
	private ListUpdater listUpdater;
	
	public GraphicInterface(ClientComm clientComm, String nickName) {
		this.clientComm = clientComm;
		this.nickName = nickName;

		microTraderFrame = new JFrame();
		Container container = microTraderFrame.getContentPane();
		
		jTableModelSell = new JTableModel(sellList);
		jTableModelPurchase = new JTableModel(purchaseList);
		jTableModelHistory = new JTableModel(new LinkedList<Order>());
		
		jTableModelSell.setType(TableType.SELLING_TABLE);
		jTableModelHistory.setType(TableType.HISTORY_TABLE);
		jTableModelPurchase.setType(TableType.PURCHASING_TABLE);
		
		jTableSell = new JTable(jTableModelSell);
		jTableHistory = new JTable(jTableModelHistory);
		jTablePurchase = new JTable(jTableModelPurchase);
		
		jMenu.add(itemDisconnect);
		jMenuBar.add(jMenu);
		microTraderFrame.setJMenuBar(jMenuBar);

		jPanelButtons = new JPanel();
		jPanelButtons.setLayout(new FlowLayout());
		jPanelButtons.add(sellUnits);
		jPanelButtons.add(purchaseUnits);
	
		jScrollPaneSell = new JScrollPane(jTableSell);
		jScrollPaneSell.setBorder(BorderFactory.createTitledBorder("Selling orders"));
		
		jScrollPaneHistory = new JScrollPane(jTableHistory);
		jScrollPaneHistory.setBorder(BorderFactory.createTitledBorder("History"));
		
		jScrollPanePurchase = new JScrollPane(jTablePurchase);
		jScrollPanePurchase.setBorder(BorderFactory.createTitledBorder("Purchasing Orders"));
		
		northEast = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true, new JScrollPane(jPanelButtons), jScrollPaneHistory);
		northEast.setDividerLocation(100);
		
		north = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, jScrollPaneSell, northEast);
		north.setDividerLocation(height/2);
		north.setResizeWeight(1.0);
		
		south= new JSplitPane(JSplitPane.VERTICAL_SPLIT, true, north,jScrollPanePurchase);
		south.setDividerLocation(height*2/5);
		
		container.add(south);

		listUpdater = ListUpdater.getInstance(sellList, purchaseList, jTableModelHistory,jTableModelSell,jTableModelPurchase, nickName);
		
		AddListeners(microTraderFrame);

		microTraderFrame.setTitle("MicroTrader "+nickName);
		microTraderFrame.setVisible(true);
		microTraderFrame.setSize(height, width);
		microTraderFrame.setLocationRelativeTo(null);
		microTraderFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		microTraderFrame.addWindowListener(new DisconnectListener(clientComm, microTraderFrame));
	}

	/**
	 * This method adds the listeners for the Menu and Bottoms.
	 * @param microTraderFrame
	 */
	private void AddListeners(JFrame microTraderFrame) {
		//Menu Listeners
		itemDisconnect.addActionListener(new DisconnectListener(clientComm, microTraderFrame));

		//Sentinela Bot�o de SellUnits		
		sellUnits.addActionListener(new SellListener(clientComm, nickName));

		//sentinela Bot�o de PurchaseUnits
		purchaseUnits.addActionListener(new PurchaseListener(clientComm, nickName));
	}
	
	public void update(Order order) {
		listUpdater.updateLists(order);
		
		
	}



	
}
